#include "../terra-ge.h"
#include <math.h>
#include <cmath>

#ifndef TERRAGE_VEC2D_H_INCLUDED
#define TERRAGE_VEC2D_H_INCLUDED

class Vec2D {
public:
   float x, y;
   Vec2D( float x = 0.0, float y = 0.0 ) : x( x ), y( y ) {}
   static inline Vec2D PolarCoords( float angle, float magnitude ) {
      return Vec2D( magnitude * std::cos( angle ), magnitude * std::sin( angle ));
   }
   inline float GetAngle() const {return std::atan2( y, x );}
   inline float GetMagnitude() const {return std::sqrt( GetMagnitudeSquared() );}
   inline float GetMagnitudeSquared() const {return x * x + y * y;}
   inline Vec2D Normalized() const {float magnitude = GetMagnitude(); return Vec2D( x / magnitude, y / magnitude );}
   inline void operator += ( const Vec2D &other ) {x += other.x; y += other.y;}
   inline void operator -= ( const Vec2D &other ) {x -= other.x; y -= other.y;}
   inline void operator *= ( float factor ) {x *= factor; y *= factor;}
   inline void operator /= ( float divisor ) {x /= divisor; y /= divisor;}
};

inline Vec2D operator + ( Vec2D first, Vec2D second ) {return Vec2D( first.x + second.x, first.y + second.y );}
inline Vec2D operator - ( Vec2D first, Vec2D second ) {return Vec2D( first.x - second.x, first.y - second.y );}
inline Vec2D operator * ( Vec2D vec, float factor )  {return Vec2D( factor * vec.x, factor * vec.y );}
inline Vec2D operator * ( float factor, Vec2D vec ) {return Vec2D( factor * vec.x, factor * vec.y );}
inline Vec2D operator / ( Vec2D vec, float divisor ) {return Vec2D( vec.x / divisor, vec.y / divisor );}
inline float operator * ( Vec2D first, Vec2D second ) {return first.x * second.x + first.y * second.y;}
inline Vec2D operator - ( Vec2D vec ) {return Vec2D( -vec.x, -vec.y );}
inline Vec2D operator ~ ( Vec2D vec ) {return vec.Normalized();}
inline bool IsCounterClockwise( const Vec2D first, const Vec2D second, const Vec2D third ) {
  float dx1, dx2, dy1, dy2;

  dx1 = second.x - first.x;
  dy1 = second.y - first.y;
  dx2 = third.x - second.x;
  dy2 = third.y - second.y;

  return dy1*dx2 < dy2*dx1;
}

#endif
